
#pragma once

#ifdef HAS_WEBRTC
#include "webrtc/common.h"

#include "webrtc/common_types.h"
#include "webrtc/modules/audio_coding/main/acm2/call_statistics.h"
#include "webrtc/modules/interface/module_common_types.h"
#include "webrtc/voice_engine/include/voe_rtp_rtcp.h"

#include "webrtc/test/channel_transport/include/channel_transport.h"
#include "webrtc/voice_engine/include/voe_audio_processing.h"
#include "webrtc/voice_engine/include/voe_base.h"
#include "webrtc/voice_engine/include/voe_codec.h"
#include "webrtc/voice_engine/include/voe_dtmf.h"
#include "webrtc/voice_engine/include/voe_file.h"
#include "webrtc/voice_engine/include/voe_hardware.h"
#include "webrtc/voice_engine/include/voe_neteq_stats.h"
#include "webrtc/voice_engine/include/voe_network.h"
#include "webrtc/voice_engine/include/voe_rtp_rtcp.h"
#include "webrtc/voice_engine/include/voe_volume_control.h"

#include "webrtc/modules/audio_coding/main/interface/audio_coding_module.h"
#include "webrtc/video_engine/include/vie_image_process.h"

#include "webrtc/modules/audio_coding/main/acm2/call_statistics.h"
#include "webrtc/modules/audio_device/audio_device_utility.h"
#include "webrtc/modules/video_capture/include/video_capture_defines.h"
#include "webrtc/video_engine/include/vie_base.h"
#include "webrtc/video_engine/include/vie_capture.h"
#include "webrtc/video_engine/include/vie_codec.h"
#include "webrtc/video_engine/include/vie_network.h"
#include "webrtc/video_engine/include/vie_render.h"
#include "webrtc/video_engine/include/vie_rtp_rtcp.h"
#include "webrtc/video_engine/vie_channel.h"
#include "webrtc/video_engine/vie_frame_provider_base.h"
#endif

#define myfn __FUNCTION__
#define myln __LINE__

//#define AFUN_ENTRY printf("enter %s line %d", __FUNCTION__,
//__LINE__),printf("\n"); #define AFUN_EXIT  printf("enter %s line %d",
//__FUNCTION__, __LINE__),printf("\n");

#if 1
#define M_AUDIO 0
#define M_VIDEO 1
#define M_RECV 0
#define M_SEND 1
#define M_DUAL 2

#define M_DEC 0
#define M_ENC 1

#define M_STOP 0
#define M_START 1
#define M_RESTART 2

#define CAMERA_STOP 0
#define CAMERA_START 1
#define CAMERA_RESTART 2
#define CAMERA_ALLOC 3
#define CAMERA_RELEASE 4
#define CAMERA_CONNECT 5
#define CAMERA_DISCONNECT 6

#define AUD_TRANS 0
#define VID_TRANS 1

#define M_LOCAL 0
#define M_REMOTE 1

#define AUD_RTPRTCP 2
#define AUD_CODEC 3
#define AUD_PROC 4
#define AUD_HARDW 5

#define VID_RTPRTCP 6
#define VID_CODEC 7
#define VID_IMAGE 8
#define VID_NETW 9

#define AUD_DTMF 10

typedef struct {
  int bytes;  // only rtp
  int packets;
} UA_RTP_INFO;

typedef struct {
  int bytes;  // include eth, ip,udp head
  int bitrate;
} UA_NET_INFO;

typedef struct tDataUnit {
  // get directly from webrtc
  UA_RTP_INFO currData[2][2];
  int outLossrate[2];
  int jitter[2];
  int64_t delay[2];
  int currframecnt[2];

#ifdef HAS_WEBRTC
  struct webrtc::AudioDecodingCallStats adec_stats;
  struct webrtc::NetworkStatistics netw_stats;
#endif

  // context need to save
  UA_RTP_INFO lastData[2][2];
  int lastframecnt[2];
  uint32_t lastTime[2];

  // output data
  UA_NET_INFO outInfo[2][2];
  int outframerate[2];

} DataUnit;

typedef struct tCodecInfo {
  int pltype;
  int w;
  int h;
  int fps;
  int maxkbps;
  int minkbps;
  int startkbps;
  char name[32];
  int (*handle_decWh_fun)(int w, int h);
} CodecInfo;

typedef struct tTransInfo {
  char ip[64];
  int port;
} TransInfo;

typedef struct tPicInfo {
  int w;
  int h;
  int fps;
  int rotate;
  bool mirror_camera;
} PicInfo;

#endif

namespace webrtc {

/*typedef struct tAudioRecordInfo
{
        int recordvoicechannelid;
        int recordingType;
        bool m_recording;

        int nWavType;
        char szTwoChnPcmFile[512];
}AudioRecordInfo;

enum VoEFileRecordingType { VoEFileRecordingTypeNone,
VoEFileRecordingTypeMicrophone, VoEFileRecordingTypePlayout};
*/

#ifndef HAS_WEBRTC
typedef void TraceCallback;
#endif

class WEBRTCIMPL {
 public:
  WEBRTCIMPL(TraceCallback* cb);
  ~WEBRTCIMPL(void);

  TraceCallback* cbPrint;

  int webrtc_api_trace(bool);
  int webrtc_api_init(void* javaVM, void* env, void* context);
  int webrtc_api_exit();
  int webrtc_api_createchannel(int media_type, int& vchn);
  int webrtc_api_deletechannel(int media_type, int& vchn);
  int webrtc_api_createtrans(int chn, int media_type, void*& trans, int& port);
  int webrtc_api_deletetrans(int media_type, void* trans, int& port);
  int webrtc_api_settrans(void* ptrans, int module, TransInfo* info = NULL);
  int webrtc_api_setcodec(int channelid, int type, int direction, void* info);
  int webrtc_api_set(int chn,
                     int module,
                     int media_direction = 0,
                     void* info = NULL);

  int webrtc_api_render(int act_type, int id, void* window = NULL);
  int webrtc_api_camera(int act_type, int& captureId, void* pInfo = NULL);
  int SetRotateCapture(int captureId, int degrees);

  int webrtc_api_startmedia(int channelid,
                            int media_direction,
                            int media_type,
                            void* info = NULL);
  int webrtc_api_stopmedia(int channelid,
                           int media_direction,
                           int media_type,
                           void* info = NULL);

  int webrtc_api_enablemic(bool);
  int webrtc_api_record(int act_type,
                        int channel,
                        int wavType = 1,
                        void* info = NULL);

  int GetAudioRtcpStatistics(int chn, DataUnit* pstDataUnit);
  int GetVideoRtcpStatistics(int chn, DataUnit* pstDataUnit);

  int getNetInfo(DataUnit* pstDataUnit, int type);

  int MediaDataGet(int type, int* pMediaData);
  int MediaDataClear(int type);

  uint64_t GetTickCount();

  int webrtc_api_dtmf(int num);

#ifdef WEBRTC_IOS
  UIView*
  createIosView(int x, int y, int width, int height, int r, int g, int b);
#endif

  int getVal(char* cmd, char* pVal);
  int getVal(char* cmd, int* pVal);
  int setVal(char* cmd, char* Val);
  int setVal(char* cmd, int Val);

#ifdef HAS_WEBRTC
  VoiceEngine* voe;
  VideoEngine* vie;
  VoEBase* audio_base;
  ViEBase* video_base;
  ViECapture* video_capture;
  ViERender* video_render;

  VoENetwork* audio_network;
  ViENetwork* video_network;
  VoERTP_RTCP* audio_rtprtcp;
  ViERTP_RTCP* video_rtprtcp;
  VoECodec* audio_codec;
  ViECodec* video_codec;

  VoEAudioProcessing* audio_ap;
  VoEHardware* audio_hardware;
  VoEDtmf* audio_dtmf;
  VoEVolumeControl* audio_vcon;
  VoENetEqStats* audio_neteqstats;
  VoEFile* audio_file;

  ViEImageProcess* video_image;

  // int rotate_;
  // bool mirror_camera;

  test::VoiceChannelTransport* audio_rtpTransport;
  test::VideoChannelTransport* video_rtpTransport;
#endif
  // webrtc::scoped_ptr <MyAudioRecorder2::Engine> pRecodingEngine;

  int audio_channelid;
  int video_channelid;
  int audio_localport;
  int video_localport;

  int rotate_;
  bool mirror_camera;

  bool mirror_local_render;
  bool mirror_remote_render;

  int m_AutoVideoSize;
  int m_iAutoLocal;

  bool m_recording;

  bool is_audio_start_sended;    //防重复,普通被叫收到ACK不必发流
  bool is_audio_start_received;  //防重复
  bool is_video_start_sended;    //防重复,普通被叫收到ACK不必发流
  bool is_video_start_received;  //防重复
};

}  // namespace webrtc

extern int setvidobj(void* javaVM);
// extern bool getvidobj();
extern void setobj(void* javaVM, void* env, void* context);
extern void clearobj();

extern int webrtc_api_setvidobj(void* javaVM);
// extern bool webrtc_api_getvidobj();
extern void webrtc_api_setobj(void* javaVM, void* env, void* context);
extern void webrtc_api_clearobj();
